<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BackendPartnerForm
 *
 * @author mirehan
 */
class BackendPartnerForm extends BasePartnerForm
{
    //put your code here
   public function configure()
   {
     // unset ($this['pid']);
      $this->widgetSchema['pid'] = new sfWidgetFormInputText();
      $this->validatorSchema['pid']=new sfValidatorString(array('max_length' => 250));
      unset($this['updated_at'],$this['created_at']);
      
      $this->widgetSchema['billfreq'] = new sfWidgetFormChoice(array(
	'choices' => array('daily' => 'daily',
    'weekly' => 'weekly',
    'monthly' => 'monthly',),
	'expanded' => true,
	));
	$this->widgetSchema['invrecfreq'] = new sfWidgetFormChoice(array(
	'choices' => array('daily' => 'daily',
    'weekly' => 'weekly',
    'monthly' => 'monthly',),
	'expanded' => true,
	));

	$this->widgetSchema['distype'] = new sfWidgetFormChoice(array(
	'choices' => array('Fixed' => 'Fixed',
    'Percentage' => 'Percentage'),
	'expanded' => true,
	));
	$this->widgetSchema->setLabels(array(
      'pid'=>'Partner ID',
      'fname'    => 'First Name',
      'lname'      => 'Last Name',
      'distype'   => 'Discount Type',
      'disvalue'   => 'Discount Value',
      'billfreq'   => 'Billing Frequency',
      'invrecfreq'   => 'Billing Receiving Frequency',
    ));   
       // create a new subcategory form for a new subcategory model object 
      if (!$this->isNew()) {
          
        // embed all subcategory forms
        $index1=0;  
        foreach ($this->getObject()->getPartneremail() as $email) {           
            // embed the subcategory form in the main category form
            $this->embedForm('email'.++$index1, new PartneremailForm($email));

            // set a custom label for the embedded form
            //$this->widgetSchema['email'.++$index]->setLabel('Subcategory: '.$subcategory->getName());

            // change the name widget to sfWidgetFormInputDelete
//            $this->widgetSchema['email'.++$index1]['email'] = new sfWidgetFormInputDelete(array(
//            'url' => 'partner/deleteEmail',      // required
//            'model_id' => $email->getEmail(),        // required
//            'confirm' => 'Sure???',                     // optional
//            ));
        }
        // embed the email form in the main partner form
        $this->embedForm('email', new PartneremailForm());
        $index2=0;
        foreach ($this->getObject()->getPartnerproducts() as $product) {           
            // embed the subcategory form in the main category form
            $this->embedForm('product'.++$index2, new PartnerproductsForm($product));
            //$this->widgetSchema['product'.++$index2]['proid']=new sfWidgetFormInputText(array(),array('value'=>$this->embeddedForms['email']->getObject()->getEmail(),'disabled'=>true));
//           $this->widgetSchema['product'.++$index2]['proid']=new sfWidgetFormInputDelete(array(
//            'url' => 'partner/deleteProduct',      // required
//            'model_id' => $product->getProid(),        // required
//            'confirm' => 'Sure???',                     // optional
//            ));
        }
        // embed the product form in the main partner form
        $this->embedForm('product', new PartnerproductsForm());
      }else{
          // embed the email form in the main partner form
          $this->embedForm('email', new PartneremailForm());
          // embed the product form in the main partner form
          $this->embedForm('product', new PartnerproductsForm());
          // embed the login form in the main partner form
          $this->embedForm('login', new LoginForm ());          
      } 
      
      ////////////////////////////////////////////
  }
  
  public function bind(array $taintedValues = null, array $taintedFiles = null) {
 
    // remove the embedded new form if the name field was not provided
    if (is_null($taintedValues['email']['email']) || strlen($taintedValues['email']['email']) === 0 ) {

        unset($this->embeddedForms['email'], $taintedValues['email']);

    // pass the new form validations
        $this->validatorSchema['email'] = new sfValidatorPass();

    } else {
        // set the category of the new subcategory form object
             $taintedValues['email']['pid']=$taintedValues['pid'];
            // echo $taintedValues['email']['pid']."   ".$taintedValues['pid'];
            // exit;
       $this->embeddedForms['email']->getObject()->
              setPartner($this->getObject());
        $this->embeddedForms['email']->getObject()->setPid($taintedValues['pid']);

     }
     if (is_null($taintedValues['product']['proid']) || strlen($taintedValues['product']['proid']) === 0 ) {

        unset($this->embeddedForms['product'], $taintedValues['product']);

    // pass the new form validations
        $this->validatorSchema['product'] = new sfValidatorPass();

    } else {
        // set the category of the new subcategory form object
        $taintedValues['product']['pid']=$taintedValues['pid'];
       $this->embeddedForms['product']->getObject()->
              setPartner($this->getObject());
        $this->embeddedForms['product']->getObject()->setPid($taintedValues['pid']);

     }
     if($this->isNew()){
        // set the category of the new subcategory form object
        $taintedValues['login']['pid']=$taintedValues['pid'];
       $this->embeddedForms['login']->getObject()->
              setPartner($this->getObject());
        $this->embeddedForms['login']->getObject()->setPid($taintedValues['pid']);
     }
    // call parent bind method
    parent::bind($taintedValues, $taintedFiles);
 
}
}

?>
